import { GameClock } from "../../../com/gengine/ui/timer/GameClock";
import { Global } from "../../../com/gengine/global/Global";
import { TimeEvent } from "../events/TimeEvent";
type int = number;
//class ClockManager
    
    export  class ClockManager /* flash.events.EventDispatcher */
    {
        constructor()
        {
            
            this._timeMap = new Map<any, any> /* flash.utils.Dictionary */();
            if (ClockManager._instance != null) 
            {
                throw new Error("ClockManager 单例");
            }
            return;
        }

        public ClockManager.get serverOpenDateNum(): int
        {
            return this._serverOpenDateNum;
        }

        public set serverOpenDate(arg1: number)
        {
            this._serverOpenDate = arg1;
            var loc1=new Date(this._serverOpenDate * 1000);
            loc1 = new Date(loc1.fullYear, loc1.month, loc1.date);
            this._serverOpenDateNum = Math.ceil((this.nowDate.time - loc1.time) / (1000 * 3600 * 24));
            this.serverOpenDayChange();
            return;
        }

        public set roleCreateDate(arg1: Date)
        {
            this._roleCreateDate = arg1;
            return;
        }

        /* internal  */initGameClock(): void
        {
            this._gameClock = new GameClock();
            this._gameClock.enterFrameHandler = this.enterFrameHandler;
            this._gameClock.x = 100;
            this._gameClock.y = 90;
            return;
        }

        public addToStage(): void
        {
            if (!this._gameClock) 
            {
                this.initGameClock();
            }
            if (this._gameClock.parent == null) 
            {
                Global.stage.addChild(this._gameClock);
            }
            return;
        }

        /* internal  */enterFrameHandler(arg1: Date): void
        {
            var loc1=null;
            var loc2=null;
            if (this._serverDate.date < arg1.date || this._serverDate.month < arg1.month || this._serverDate.fullYear < arg1.fullYear) 
            {
                this._serverDate.time = arg1.time;
                dispatchEvent(new TimeEvent(TimeEvent.DateChange));
                var loc3;
                var loc4=((loc3 = this)._serverOpenDateNum + 1);
                loc3._serverOpenDateNum = loc4;
                this.serverOpenDayChange();
            }
            loc3 = 0;
            loc4 = this._timeMap;
            for (loc2 in loc4) 
            {
                loc1 = loc2 as Date;
                if (!loc1) 
                {
                    continue;
                }
                if (!(loc1.time <= arg1.time)) 
                {
                    continue;
                }
                var loc5;
                (loc5 = this._timeMap)[loc1]();
                this.removeDateCall(loc1);
            }
            return;
        }

        public addDateCall(arg1: Date, arg2: Function): void
        {
            if (typeof arg2 === "function") 
            {
                if (arg1.time > this._gameClock.nowDate.time) 
                {
                    this._timeMap[arg1] = arg2;
                }
            }
            return;
        }

        public removeDateCall(arg1: Date): void
        {
            if (this._timeMap[arg1]) 
            {
                this._timeMap[arg1] = null;
                delete this._timeMap[arg1];
            }
            return;
        }

        /* internal  */serverOpenDayChange(): void
        {
            dispatchEvent(new TimeEvent(TimeEvent.ServerOpenDayChange));
            return;
        }

        public setNowTimer(arg1: Object): void
        {
            if (this._gameClock == null) 
            {
                this._gameClock = new GameClock();
                this._gameClock.enterFrameHandler = this.enterFrameHandler;
                this._gameClock.x = 100;
                this._gameClock.y = 90;
            }
            this._gameClock.time = arg1;
            this._serverDate = new Date(this._gameClock.nowDate.time);
            return;
        }

        public ClockManager.get nowDate(): Date
        {
            if (!this._gameClock) 
            {
                this.initGameClock();
            }
            return this._gameClock.nowDate;
        }

        public getLoginDayNum(): int
        {
            var loc1=NaN;
            if (this._roleCreateDate && this._gameClock.nowDate) 
            {
                loc1 = this._roleCreateDate.time - this._gameClock.nowDate.time;
                return Math.ceil(loc1 / this.DayTime);
            }
            return 1;
        }

        public isNotCreateRoleDay(): Boolean
        {
            if (this._roleCreateDate && this._gameClock.nowDate) 
            {
                return !(this._roleCreateDate.date == this._gameClock.nowDate.date) || !(this._roleCreateDate.month == this._gameClock.nowDate.month) || !(this._roleCreateDate.fullYear == this._gameClock.nowDate.fullYear);
            }
            return false;
        }

        public getRunningTime(): number
        {
            return this._gameClock.nowDate.time - this._serverDate.time;
        }

        public ClockManager.get serverOpenDate(): number
        {
            return this._serverOpenDate;
        }

        public static get instance(): ClockManager
        {
            if (ClockManager._instance == null) 
            {
                ClockManager._instance = new ClockManager();
            }
            return ClockManager._instance;
        }

        /* internal */ /* const */DayTime: number=86400000;

        /* internal */ /* var */_gameClock: GameClock;

        /* internal */ /* var */_roleCreateDate: Date;

        /* internal */ /* var */_timeMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_serverDate: Date;

        /* internal */ /* var */_serverOpenDate: number=0;

        /* internal */ /* var */_serverOpenDateNum: int;

        /* internal */ static /* var */_instance: ClockManager;
    }
